import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:wan_android_flutter/utils/image_utils.dart';

class ImagePickerUtils {
  // 打开相机拍照
  static Future<void> pickImageFromCamera(BuildContext context, Function(String) onImagePicked) async {
    final ImagePicker _picker = ImagePicker();
    final XFile? image = await _picker.pickImage(source: ImageSource.camera);
    if (image != null) {
      onImagePicked(image.path);
    } else {
      print('未拍照');
    }
    Navigator.pop(context); // 关闭弹窗
  }

  // 从相册选择图片
  static Future<void> pickImageFromGallery(BuildContext context, Function(String) onImagePicked) async {
    final ImagePicker _picker = ImagePicker();
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      final directory = await getApplicationDocumentsDirectory();
      String path = '${directory.path}/headImage${DateTime.now().toString()}.jpg'; // 设置保存的文件路径
      await ImageUtils.saveImage(File(image.path), path);
      onImagePicked(path);
    } else {
      print('未选择图片');
    }
    // Navigator.pop(context); // 关闭弹窗
  }
}
